package nodelist

// https://leetcode-cn.com/leetbook/read/top-interview-questions-medium/xvijdh/
// 连接同层的节点
// 算法: 直接通过数组遍历即可
func connect(root *Node) *Node {
	if nil == root {
		return nil
	}
	currentNodeArr := make([]*Node, 0)
	currentNodeArr = append(currentNodeArr, root)
	for 0 != len(currentNodeArr) {
		newNodeArr := make([]*Node, 0)
		for index, currentNode := range currentNodeArr {
			if nil != currentNode.Left {
				newNodeArr = append(newNodeArr, currentNode.Left)
			}
			if nil != currentNode.Right {
				newNodeArr = append(newNodeArr, currentNode.Right)
			}
			if index < len(currentNodeArr)-1 {
				currentNode.Next = currentNodeArr[index+1]
			}
		}
		currentNodeArr = newNodeArr
	}
	return root
}

type Node struct {
	Val   int
	Left  *Node
	Right *Node
	Next  *Node
}
